using System;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity.ObjectBuilder;

namespace Common.Unity.Extensions
{
	public static class IStagedStrategyChainExtensions
	{
		public static void AddNewOnce<TStrategy>( this StagedStrategyChain<UnityBuildStage> target, UnityBuildStage stage )
			where TStrategy : IBuilderStrategy, new()
		{
			if ( !target.Contains<TStrategy>( ) )
			{
				target.AddNew<TStrategy>( stage );
			}
		}

		public static bool Contains<StrategyType>( this IStagedStrategyChain target ) where StrategyType : IBuilderStrategy
		{
			return Contains( target, typeof(StrategyType) );
		}

		public static bool Contains( IStagedStrategyChain target, Type type )
		{
			foreach ( IBuilderStrategy strategy in target.MakeStrategyChain() )
			{
				if ( strategy.GetType() == type )
				{
					return true;
				}
			}
			return false;
		}

	}
}